var moduleRoleModule = angular.module('moduleRoleModule', ['paginationService', 'anajaxService','agularValidate','innerPaginationService']);

moduleRoleModule.controller('ModuleRoleController', function($scope, $http,pagination,anajax,innerPagination) {
	$scope.resultList={};
	$scope.queryForm={};	
	$scope.moduleroleForm={};
	$scope.rolePurview={};
	
	
	/**
	 * 跳转到指定页
	 */
	$scope.goPage = function(selNum) {
		
		pagination.goPage(selNum);
		query($scope.pagination);
	};
	
	/**
	 * 上一页
	 */
	$scope.lastPage = function() {
		pagination.lastPage();
		query($scope.pagination);
	};
	
	/**
	 * 下一页
	 */
	$scope.nextPage = function() {
		pagination.nextPage();
		query($scope.pagination);
	};
	
	 /**
	 * 查询
	 */
	var query = function(params) {
		var url = commonutil.actionPath + '/module/role/rolelist';
		anajax.doajax(url, params, function(data) {
			$scope.resultList = data.resultList;
			pagination.setData(data.pagination);
			$scope.pagination = data.pagination; 
		});
	};
	
	/**
	 * 点击查询
	 */
	$scope.doquery = function() {
		
		query($scope.queryForm);
	};
	
	query($scope.queryForm);
	
	
	/**初始化新增*/
	$scope.goAdd = function(index) {
		$scope.moduleroleForm = {
		};
		
		$scope.moduleroleForm.isDefaultRole=1;
		$scope.stations = [];
		$scope.rightPart = 'add';
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('新增模块角色');
	};
	
	
	/**初始化模块角色*/
	$scope.goModify  = function(index) {
		$scope.rightPart = 'edit';
		var assets=$scope.resultList[index];
		angular.copy(assets,$scope.moduleroleForm);
		$scope.moduleroleForm.createTime=null;
		$scope.moduleroleForm.lastUpdateTime=null;
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('编辑模块角色');
	};
	
	$scope.doCancel = function(){
		pageutil.hideRightBar();
	}
	
	
	/**
	 * 执行添加
	 */
	$scope.doAdd = function() {
		
		var res = Validator.validate($("#edit-data-warp"));
		if(!($scope.moduleroleForm.moduleInfoId!=null&&$scope.moduleroleForm.moduleInfoId!="")){
			pageutil.showTip('请选择模块');
			res=false;
		}
		if(!res){
			return;
		}
		
		
		if($scope.rightPart=='add'){
			$scope.add($scope.moduleroleForm);
		}else{
			$scope.modify($scope.moduleroleForm);
		}
	};
	
	
	/**
	 * 执行添加
	 */
	$scope.add = function(postData) {
		var url = commonutil.actionPath + '/module/role/add';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	/**
	 * 执行修改
	 */
	$scope.modify = function(postData) {
		
		var url = commonutil.actionPath + '/module/role/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('修改成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
$scope.delBean ={};
	
	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWin = function($index){
		var assetBean = $scope.resultList[$index];
		$scope.delBean= assetBean;
		$scope.moduleroleForm.createTime=null;
		$scope.moduleroleForm.lastUpdateTime=null;
		//$scope.delBean.useTime=null;
		$("#confirm_del_asset_div").modal('show');
	};
	
	$scope.doDelAsset = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/module/role/del';
		anajax.doajax(url,{id:$scope.delBean.moduleRoleId}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	$scope.rolePurview = {};
	//菜单页面展现
		$scope.goResourceModify = function(index) {
			var role = $scope.resultList[index];
			$scope.init(role.moduleRoleId);
			$scope.rolePurview.roleid = role.moduleRoleId;
			$("#confirm_addMenu_data_div").modal('show');
			$scope.resourceTitle = role.roleName;
		};
	
		
		var setting = {
				check: {
					enable: true,
					chkboxType:{ "Y" : "ps", "N" : "ps"}
				},
				data: {
					simpleData: {
						enable: true
					}
				}
			};
		
		/**初始化树*/
		$scope.init = function(roleId) {
			var url = commonutil.actionPath + '/module/role/purview';
			anajax.doajax(url, {'id':roleId}, function(data) {
				var zTree =  $.fn.zTree.init($("#tree"), setting, data.treeData);
				for(p in data.purviews){
					var node = zTree.getNodeByParam("id", data.purviews[p], null);
					zTree.checkNode(node, true, true);
				}
				zTree.expandAll(true);
			}); 
			
		};
		
		$scope.saveRolePurviews = function(){
			var zTree = $.fn.zTree.getZTreeObj("tree")
			var nodes = zTree.getCheckedNodes(true);
			var chkNode="";
			for(i in nodes){
				if(nodes[i].id!=null && nodes[i].id!=""){
					if(i==nodes.length-1)
						chkNode+=nodes[i].id;
					else
						chkNode+=nodes[i].id+",";
				}
			}
			var url = commonutil.actionPath + '/module/role/purviewSave';
			anajax.doajax(url,{id:$scope.rolePurview.roleid,purid:chkNode}, function(data) {
				if (data.resultValue ) {
					pageutil.showTip('保存成功');
					//$scope.doquery();
				} else {
					pageutil.showTip(data.message);
				}
			});
		}
	
});